<template>
  <div class="login_page">
    <transition name="form-fade" mode="in-out">
      <section class="form_contianer" v-show="showLogin">
        <div class="manage_tip">
          <span class="title">后台管理系统</span>
        </div>
        <el-form :model="loginForm" :rules="rules" ref="loginForm" class="loginForm">
          <el-form-item prop="username">
            <el-input placeholder="用户名" prefix-icon="fa fa-user" v-model="loginForm.username" ></el-input>
          </el-form-item>
          <el-form-item prop="password">
            <el-input type="password" prefix-icon="fa fa-lock" placeholder="密码" v-model="loginForm.password"></el-input>
          </el-form-item>
          <el-row>
            <el-col :span="16">
              <el-form-item>
                <el-button type="primary"  @click="submitForm('loginForm')" class="submit_btn">登录</el-button>
              </el-form-item>
            </el-col>
            <el-col :span="2" :offset="2">
              <el-button type="text" @click="dialogFormVisible = true">忘记密码？</el-button>
            </el-col>
          </el-row>
        </el-form>
      </section>
    </transition>
    <el-dialog title="修改密码" center :visible.sync="dialogFormVisible" width="30%" :close-on-click-modal="false">
      <el-form :model="pwdForm" label-width="100px" :rules="pwdFormRules" ref="pwdForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="pwdForm.username"></el-input>
        </el-form-item>
        <el-form-item label="新密码" prop="firstPwd">
          <el-input type="password" v-model="pwdForm.firstPwd" autocomplete="off"></el-input>
        </el-form-item>
        <el-form-item label="确认密码" prop="secondPwd">
          <el-input type="password" autocomplete="off" v-model="pwdForm.secondPwd"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="closeUpdatePwd">取 消</el-button>
        <el-button type="primary" @click="updatePwd('pwdForm')">确 定</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import api from './api';

export default {
  data() {
    const regStr = /^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,16}$/;
    const validatePwd = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请输入密码'));
      } else if (!value.match(regStr)) {
        callback(new Error('密码至少6位，且需由字母和数字组成'));
      } else {
        if (!this.pwdForm.secondPwd) {
          this.$refs.pwdForm.validateField('secondPwd');
        }
        callback();
      }
    };
    const validateSecPwd = (rule, value, callback) => {
      if (!value) {
        callback(new Error('请再次输入密码'));
      } else if (value !== this.pwdForm.firstPwd) {
        callback(new Error('两次输入密码不一致'));
      } else {
        callback();
      };
    };
    return {
      dialogFormVisible: false,
      pwdForm: {
        username: '',
        firstPwd: '',
        secondPwd: ''
      },
      pwdFormRules: {
        username: [
          { required: true, message: '用户名不能为空', trigger: 'change' }
        ],
        firstPwd: [
          { required: true, validator: validatePwd, trigger: 'blur' }
        ],
        secondPwd: [
          { required: true, validator: validateSecPwd, trigger: 'blur' }
        ]
      },
      loginForm: {
        username: '',
        password: ''
      },
      rules: {
        username: [
          { required: true, message: '请输入用户名', trigger: 'blur' },
          { min: 2, max: 15, message: '长度在 2 到 15 个字符', trigger: 'blur' }
        ],
        password: [
          { required: true, message: '请输入密码', trigger: 'blur' }
        ]
      },
      showLogin: true
    };
  },
  methods: {
    closeUpdatePwd() {
      this.pwdForm = {
        username: '',
        firstPwd: '',
        secondPwd: ''
      };
      this.dialogFormVisible = false;
    },
    updatePwd(pwdForm) {
      this.$refs[pwdForm].validate((valid) => {
        if (valid) {
          const pwdObj = { username: this.pwdForm.username, password: this.pwdForm.firstPwd };
          api.updatePwd(pwdObj).then((data) => {
            if (data.flag) {
              this.$message({
                type: 'success',
                message: data.message
              });
              this.pwdForm = {
                username: '',
                firstPwd: '',
                secondPwd: ''
              };
              this.dialogFormVisible = false;
            } else {
              this.$message({
                type: 'warning',
                message: data.message
              });
            }
          });
        }
      });
    },
    submitForm(formName) {
      this.$refs[formName].validate((valid) => {
        if (valid) {
          const loginParams = { username: this.loginForm.username, password: this.loginForm.password };
          api.login(loginParams).then((data) => {
            if (data.flag === 1) {
              this.$message({
                type: 'success',
                message: '登录成功'
              });
              if (process.env.NODE_ENV === 'production') {
                window.location.href = '/managerSystem/index';
              } else {
                window.location.href = '/manageSystem';
              }
            } else {
              this.$message({
                type: 'error',
                message: '用户名或密码错误，请重新输入'
              });
              return false;
            }
          });
        } else {
          this.$message({
            type: 'warning',
            message: '登陆失败'
          });
          return false;
        }
      });
    }
  }
};
</script>

<style lang="scss" scoped>
  .login_page{
    position: absolute;
    width: 100%;
    height: 100%;
    background: url('./images/background.jpg') no-repeat center center;
    background-size: 100% 100%;
  }
  .loginForm{
    background-color: #fff;
    padding:20px;
    border-radius:3px;
    box-shadow: 5px 5px 10px #01144c;
  }
  .manage_tip{
    margin-bottom:20px;
    .title{
      font-family: cursive;
      font-weight: bold;
      font-size: 26px;
      color:#fff;
    }
  }
  .form_contianer{
    width:20%;
    height:210px;
    position: absolute;
    top: 20%;
    left: 38%;
    padding: 25px;
    border-radius: 5px;
    text-align: center;
    .submit_btn{
      width: 100%;
      font-size: 16px;
      letter-spacing:20px;
    }
  }
  .tiparea{
    text-align:left;
    font-size: 12px;
    color: #4cbb15;
    .tip{
      margin-left: 54px;
    }
  }

  .form-fade-enter-active, .form-fade-leave-active {
    transition: all 1s;
  }
  .form-fade-enter, .form-fade-leave-active {
    transform: translate3d(0, -50px, 0);
    opacity: 0;
  }
  .loginForm{
    .el-button--primary{
      background-color:#FF7C1A;
      border:1px solid #FF7C1A;
    }
  }
</style>
